package cn.zhaoyuening.hnistalumnirecord.web.controller.admin;

import cn.zhaoyuening.hnistalumnirecord.domain.NewsModule;
import cn.zhaoyuening.hnistalumnirecord.web.controller.BaseController;
import cn.zhaoyuening.hnistalumnirecord.web.controller.ModuleController;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by Zhao on 2017/2/21.
 */
@Controller
@RequestMapping("admin/module/")
public class AdminModuleController extends BaseController {
    @RequestMapping("moduleManagerUi.do")
    public String moduleManagerUi() {
        return "admin/moduleManager";
    }

    @RequestMapping("createModuleUi.do")
    public String createModuleUi() {
        return "admin/createModule";
    }

    @RequestMapping("createModule.do")
    @ResponseBody
    public String createModule(Integer id,String name,Integer type, Model model) {
        if(type==null|| StringUtils.isEmpty(name)){
            return "fail";
        }
        NewsModule module = new NewsModule(name);
        module.setId(id);
        module.setType(type);
        getModuleService().save(module);
        return "success";
    }

    //删除module
    @RequestMapping("removeModules.do")
    @ResponseBody
    public String removeModules(@RequestParam Integer id) {
        NewsModule module = new NewsModule();
        module.setId(id);
        getModuleService().delete(module);
        return "success";
    }

    //删除module
    @RequestMapping("cleanModules.do")
    @ResponseBody
    public String cleanModules(@RequestParam Integer id) {
        getModuleService().cleanModule(id);
        return "success";
    }

    @RequestMapping("editModuleUi.do")
    public String editModuleUi(@RequestParam Integer id,Model model) {
        NewsModule module = getModuleService().findById(id);
        model.addAttribute("module", module);
        return "admin/editModule";
    }

    @RequestMapping("editModule.do")
    @ResponseBody
    public String editModule(@ModelAttribute NewsModule module,Model model) {
        if (module.getId()==null||StringUtils.isEmpty(module.getName())||module.getType()==null){
            return "fail";
        }
        getModuleService().addOrEdit(module);
        return "success";
    }
}
